from logging import getLogger

import openpyxl

from ..receipt import RECEIPT_ITEM_NAME

logger = getLogger(__name__)


def export_to_excel(excel_path: str, receipts: list[dict]) -> None:
    # 创建一个 Excel 文件
    workbook = openpyxl.Workbook()
    sheet = workbook.active

    # 写入表头
    headers = list(RECEIPT_ITEM_NAME.values())
    for col_idx, header in enumerate(headers, 1):
        sheet.cell(row=1, column=col_idx, value=header)

    # 写入数据
    for row_idx, receipt in enumerate(receipts, 2):
        for col_idx, key in enumerate(RECEIPT_ITEM_NAME.keys(), 1):
            sheet.cell(row=row_idx, column=col_idx, value=receipt.get(key, ""))

    # 保存文件
    workbook.save(excel_path)
